package com.ocss.system.service.impl;

import java.time.LocalDate;
import java.time.Period;
import java.util.List;

import com.ocss.common.core.domain.entity.SysUser;
import com.ocss.common.utils.DateUtils;
import com.ocss.common.utils.SecurityUtils;
import com.ocss.system.mapper.SysUserMapper;
import com.ocss.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ocss.system.mapper.OcssStudentMapper;
import com.ocss.system.domain.OcssStudent;
import com.ocss.system.service.IOcssStudentService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 学生信息Service业务层处理
 *
 * @author ligen
 * @date 2024-03-06
 */
@Service
public class OcssStudentServiceImpl implements IOcssStudentService {
    @Autowired
    private OcssStudentMapper ocssStudentMapper;

    @Resource
    private SysUserServiceImpl sysUserService;

    @Resource
    private ISysConfigService configService;

    @Resource
    private SysUserMapper userMapper;

    /**
     * 查询学生信息
     *
     * @param id 学生信息主键
     * @return 学生信息
     */
    @Override
    public OcssStudent selectOcssStudentById(Long id) {
        return ocssStudentMapper.selectOcssStudentById(id);
    }

    /**
     * 查询学生信息列表
     *
     * @param ocssStudent 学生信息
     * @return 学生信息
     */
    @Override
    public List<OcssStudent> selectOcssStudentList(OcssStudent ocssStudent) {
        return ocssStudentMapper.selectOcssStudentList(ocssStudent);
    }

    /**
     * 新增学生信息
     *
     * @param ocssStudent 学生信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertOcssStudent(OcssStudent ocssStudent) {
        ocssStudent.setCreateTime(DateUtils.getNowDate());

        // 判断该用户的手机号是否重复
        String p = ocssStudent.getPhone();
        OcssStudent ocssStudent1 = new OcssStudent();
        ocssStudent1.setPhone(p);
        List<OcssStudent> ocssStudents = ocssStudentMapper.selectOcssStudentList(ocssStudent1);
        if (!ocssStudents.isEmpty()) {
            return -1;
        }

        // 根据身份证信息计算年龄和性别，同时将学生添加到用户表中，并默认分配学生的角色，学生默认密码设置为身份证后六位+系统密码参数
        String idCard = ocssStudent.getIdCard();

        ocssStudent.setAge(this.calculateAge(idCard));
        ocssStudent.setSex(this.getGender(idCard));
        // 身份证后六位
        String lastSixDigits = idCard.substring(idCard.length() - 6);
        // 系统初始化密码  这里需要将密码和身份证后六位进行拼接后加密
        String initPassword = configService.selectConfigByKey("sys.user.initPassword");
        String password = SecurityUtils.encryptPassword(lastSixDigits + initPassword);

        // 将学生对象组装为用户对象
        SysUser sysUser = new SysUser();
        sysUser.setDeptId(ocssStudent.getDeptId());
        sysUser.setUserName(ocssStudent.getPhone());
        sysUser.setNickName(ocssStudent.getName());
        sysUser.setPhonenumber(ocssStudent.getPhone());
        sysUser.setSex(ocssStudent.getSex());
        sysUser.setPassword(password);
        sysUser.setRoleIds(new Long[]{101L});
        // 设置用户权限为学生
        int row = ocssStudentMapper.insertOcssStudent(ocssStudent);
        sysUser.setPid(ocssStudent.getId());
        userMapper.insertUser(sysUser);
        sysUserService.insertUserRole(sysUser);


        return row;
    }

    // 获取性别
    public String getGender(String idCard) {
        // 假设身份证号码的倒数第二位表示性别，奇数为男，偶数为女
        int genderDigit = Integer.parseInt(idCard.substring(idCard.length() - 2, idCard.length() - 1));

        return genderDigit % 2 == 0 ? "1" : "0";
    }

    // 计算年龄
    public String calculateAge(String idCard) {
        // 假设身份证号码的格式为年月日，如19900101
        int birthYear = Integer.parseInt(idCard.substring(6, 10));
        int birthMonth = Integer.parseInt(idCard.substring(10, 12));
        int birthDay = Integer.parseInt(idCard.substring(12, 14));

        LocalDate birthdate = LocalDate.of(birthYear, birthMonth, birthDay);
        LocalDate currentDate = LocalDate.now();
        Period period = Period.between(birthdate, currentDate);

        return String.valueOf(period.getYears());
    }

    /**
     * 修改学生信息
     *
     * @param ocssStudent 学生信息
     * @return 结果
     */
    @Override
    public int updateOcssStudent(OcssStudent ocssStudent) {
        ocssStudent.setUpdateTime(DateUtils.getNowDate());
        return ocssStudentMapper.updateOcssStudent(ocssStudent);
    }

    /**
     * 批量删除学生信息
     *
     * @param ids 需要删除的学生信息主键
     * @return 结果
     */
    @Override
    public int deleteOcssStudentByIds(Long[] ids) {
        return ocssStudentMapper.deleteOcssStudentByIds(ids);
    }

    /**
     * 删除学生信息信息
     *
     * @param id 学生信息主键
     * @return 结果
     */
    @Override
    public int deleteOcssStudentById(Long id) {
        return ocssStudentMapper.deleteOcssStudentById(id);
    }
}
